package com.bootcamp.servlet.mvc.utils;

import com.bootcamp.servlet.mvc.model.Custom;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

/**
 * @program: wrok-1101
 * @description: auth whether a user has login or not
 * @author: Keyu Li
 * @create: 2021-11-05 21:37
 **/

public class PrintHeadByAuthUtils {

    /**
     *
     * @param request:
     * @param writer:
     * @return void
     * @date 11/6/2021 10:07
     */
    public static void printHead(HttpServletRequest request, PrintWriter writer) {
        // 获取userid
        HttpSession session = request.getSession();
        Custom custom = (Custom) session.getAttribute("custom");
        String contextPath = request.getContextPath();
        String homepage =contextPath;
        String storepage = contextPath + "/store/page";
        writer.println("<div><a href='" + homepage + "'>首页</a>&nbsp;&nbsp;&nbsp;<a href='"+ storepage + "'>商品列表</a>&nbsp;&nbsp;&nbsp;");

        if (custom == null) {
            // 没有用户登录：主页 商品列表 注册 登录
            String register = contextPath + "/storev2/register_v2.html";
            String login = contextPath + "/storev2/login_v2.html";

            writer.println("<a href='"+register+"'>注册</a>&nbsp;&nbsp;&nbsp;<a href='"+login+"'>登录</a></div>");
            return;
        }
        // 有用户登录： 主页 商品列表 欢迎 退出 个人主页
        String logoff = contextPath + "/user/logoff";
        String userpage = contextPath+ "/user/homepage";
        writer.println("欢迎您，高贵的 " + custom.getUsername() +"(id:"+custom.getId()+")"
                + "&nbsp;&nbsp;&nbsp;<a href='"+userpage
                +"'>个人主页</a>&nbsp;&nbsp;&nbsp;<a href='"+logoff
                +"'>退出</a></div></div>");
    }
}
